package com.luofan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.luofan.common.constant.GroupConstant;
import com.luofan.common.result.CustomException;
import com.luofan.common.result.ResultCodeEnum;
import com.luofan.common.util.UserContext;
import com.luofan.entity.dto.NoticeUserDTO;
import com.luofan.entity.dto.PageDTO;
import com.luofan.entity.po.Notice;
import com.luofan.entity.po.NoticeUser;
import com.luofan.entity.po.User;
import com.luofan.entity.vo.NoticeUserVO;
import com.luofan.entity.vo.NoticeVO;
import com.luofan.mapper.NoticeMapper;
import com.luofan.mapper.UserMapper;
import com.luofan.service.NoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 公告表 服务实现类
 * </p>
 *
 * @author 
 * @since 2025-03-26
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private NoticeUserServiceImpl noticeUserService;

    //发布通知
    @Override
    @Transactional  //开启事务
    public ResultCodeEnum createNotice(Notice notice) {
        //获取当前时间为发布时间
        notice.setTime(LocalDateTime.now());
        //获取当前用户id为发布者id
        Long currentUserId = UserContext.getCurrentUserId();
        notice.setPublisherId(currentUserId);

        //添加通知信息到notice表
        int rows = noticeMapper.saveNotice(notice);
        if (rows == 0) return ResultCodeEnum.FAIL;
        //获取刚刚添加的notice的Id
        Long noticeId = notice.getId();

        //根据通知的人群将信息添加到notice_member表
        List<Long> userIdList = null;
        if (notice.getGroup() == GroupConstant.ALL) {
            //获取除了管理员外的所有用户id
            userIdList = userMapper.listAllUserId();
        } else if (notice.getGroup() == GroupConstant.HEAD) {
            //获取所有主任id
            userIdList = userMapper.listHeadId();
        } else if (notice.getGroup() == GroupConstant.TEACHER) {
            //获取所有教师id
            userIdList = userMapper.listTeacherId();
        }
        //批量添加到notice_member表
        List<NoticeUser> list = new ArrayList<>();
        for (Long userId : userIdList) {
            list.add(new NoticeUser().setNoticeId(noticeId).setUserId(userId));
        }
        // 一次最多批量添加100条数据
        boolean saveBatch = noticeUserService.saveBatch(list, 100);
        //插入错误抛出异常
        if (!saveBatch) throw new CustomException(ResultCodeEnum.SAVE_ERROR);

        return ResultCodeEnum.SUCCESS;
    }

    //管理员 获取所有公告的列表
    @Override
    public PageInfo<NoticeVO> listAllNotice(PageDTO pageDTO) {
        //进行分页拦截
        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());

        //查询
        List<NoticeVO> noticeVOList = noticeMapper.listAllNotice();

        //获取PageInfo返回
        return new PageInfo<>(noticeVOList);
    }

    //根据用户id获取公告列表
    @Override
    public PageInfo<NoticeVO> listNoticeByUserId(Long id, PageDTO pageDTO) {
        //进行分页拦截
        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());

        //查询
        List<NoticeVO> noticeVOList = noticeMapper.listNoticeByUserId(id);

        //获取PageInfo返回
        return new PageInfo<>(noticeVOList);
    }

    //用户确认收到公告
    @Override
    public void confirmReceiveNotice(Long noticeId) {
        Long currentUserId = UserContext.getCurrentUserId();
        noticeMapper.confirmReceiveNotice(currentUserId, noticeId);
    }

    //通过公告id获取所有收到和没收到公告的用户信息(姓名,邮箱,教研室名称)列表
    @Override
    public NoticeUserVO listNoticeUserByNoticeId(Long id) {
        //先获取所有收到和没收到公告的用户信息(姓名,邮箱,教研室名称,是否收到通知)
        List<NoticeUserDTO> noticeUserDTOList = noticeMapper.listNoticeUserByNoticeId(id);

        //根据isReceived是否收到通知分别将用户封装到NoticeUserVO的收到和没收到两个集合中
        NoticeUserVO noticeUserVO = new NoticeUserVO();

        for (NoticeUserDTO noticeUserDTO : noticeUserDTOList) {
            //没收到通知的
            if (noticeUserDTO.getIsReceived() == 0) {
                noticeUserVO.getNotReceivedUserList().add(noticeUserDTO);
            }
            //收到通知的
            else if (noticeUserDTO.getIsReceived() == 1) {
                noticeUserVO.getReceivedUserList().add(noticeUserDTO);
            }
        }

        return noticeUserVO;
    }


}
